﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using System.Windows;

using Sogeti.QueryWrapper;
using System.Windows.Data;
using System.ComponentModel;
using Sogeti.VSExtention;

namespace Mskold.IterationManager
{
    class NewIterationViewModel :INotifyPropertyChanged
    {
        public const string str_NEW_ITERATION_MARK = "INSERT NEW ITERATION";
        public NewIterationViewModel()
        {
            StartDate = DateTime.Now;
            FinishDate = DateTime.Now.AddDays(21);

            Actions = new List<IAction>();
        }
        public NewIterationViewModel(IterationManagerConfig cfg)
        {
            Actions = new List<IAction>();
            Config = cfg;
        }

        public void Load(TeamExplorerIntergator te)
        {

            teamExplorer = te;
            Config= new IterationManagerConfig(teamExplorer);
            Config.Load();

            StartDate = DateTime.Now;
            FinishDate = DateTime.Now.AddDays(21);

            Sogeti.QueryWrapper.CommonStructureWrapper csw = new Sogeti.QueryWrapper.CommonStructureWrapper(teamExplorer.tpCollection);
            IterationsXml = csw.GetNodesTree(Sogeti.QueryWrapper.StructureType.Iteration, teamExplorer.tpName);
          

            //Add INSERT MARK node 
            _xInsertMark= IterationsXml.OwnerDocument.CreateElement("Node");
            
            
            XmlAttribute aName = IterationsXml.OwnerDocument.CreateAttribute("Name");
            aName.Value = str_NEW_ITERATION_MARK;
            _xInsertMark.Attributes.Append(aName);

            XmlAttribute aNodeId = IterationsXml.OwnerDocument.CreateAttribute("NodeID");
            aNodeId.Value = str_NEW_ITERATION_MARK;
            _xInsertMark.Attributes.Append(aNodeId);

            IterationsXml.FirstChild.FirstChild.AppendChild(_xInsertMark);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(IterationsXml.OuterXml);

            Iterations = doc;


            Actions.Add(new CreateIterationAction());

            if (Config.ConfigStore.CreateSprintWorkItem)
            {
                Actions.Add(new CreateWorkItemAction());
            }
            if (Config.ConfigStore.CreateSprintFolder)
            {
                Actions.Add(new CopyIterationFolderAction());

            }

        }

        public IEnumerable<string> ActionTitles { get { return from a in Actions select a.Title; } }
              
        public string IterationName
        {
            get   { return _IterationName;}

            set
            {
                _IterationName = value;
                NotifyPropertyChanged("IterationName");
            }
        }

        public DateTime StartDate
        {
            get { return _StartDate; }

            set
            {
                _StartDate = value;
                NotifyPropertyChanged("StartDate");
            }
        }

        public DateTime FinishDate
        {
            get { return _FinishDate; }

            set
            {
                _FinishDate = value;
                NotifyPropertyChanged("FinishDate");
            }
        }


        public IterationManagerConfig Config
        {
            get { return _config; }
            set { _config = value; }
        }
        public XmlElement IterationsXml
        {
            get {return _xml;}
            set {_xml=value;}

        }

        public XmlDocument Iterations
        {
            get { return _xmlDoc; }
            set { _xmlDoc = value; }

        }

        public string SelectedIterationValurPath
        {
            get
            {
                return _SelectedIterationValurPath;
            }

            set
            {
                _SelectedIterationValurPath = value; 
                NotifyPropertyChanged("SelectedIterationValurPath");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;


        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
        
  
        private TeamExplorerIntergator teamExplorer;
        private IterationManagerConfig _config;
        private XmlElement _xml;
        private XmlDocument _xmlDoc;

        private string _IterationName;
        private DateTime _StartDate;
        private DateTime _FinishDate;


        internal void cmdIterationUp(XmlElement xNode)
        {

            XmlElement xParent = (XmlElement)xNode.ParentNode;
            XmlNode xPreviousSibling= xNode.PreviousSibling;
            if(xPreviousSibling!=null)
            {
                
                XmlNode x = xNode.PreviousSibling;
                xParent.ReplaceChild(xNode, xNode.PreviousSibling);
                
                
                xParent.InsertAfter(x, xNode);
                
                
            }
            
        }

        internal void cmdIterationDown(XmlElement xNode)
        {
            XmlElement xParent = (XmlElement)xNode.ParentNode;
            XmlNode xNextSibling = xNode.NextSibling;
            if (xNextSibling != null)
            {
                xParent.RemoveChild(xNextSibling);
                xParent.InsertBefore(xNextSibling, xNode);
            }
        }

        internal void cmdIterationIndent(XmlElement xNode)
        {
            XmlElement xParent = (XmlElement)xNode.ParentNode;
            XmlNode xPrevSibling = xNode.PreviousSibling;
            if (xPrevSibling != null)
            {
                XmlNode xChilds = xPrevSibling.FirstChild;
                if (xChilds == null)
                {
                    xChilds = xPrevSibling.OwnerDocument.CreateElement("Children");
                    xPrevSibling.AppendChild(xChilds);
                }
                xChilds.AppendChild(xNode);
                    
            }

            //TreeViewItem item = ((TreeViewItem)treeView1.SelectedItem);
            //if (item.Parent is TreeViewItem)
            //{
            //    TreeViewItem parent = ((TreeViewItem)item.Parent);
            //    int ix = parent.Items.IndexOf(item);

            //    parent.Items.Remove(item);
            //    ((TreeViewItem)parent.Items.GetItemAt(ix - 1)).Items.Add(item);
            //    item.IsSelected = true;
            //    item.IsExpanded = true;
            //}

        }

        internal void cmdIterationOutdent(XmlElement xNode)
        {
            XmlElement xParent = (XmlElement)xNode.ParentNode;

            XmlElement xGrandParent = (XmlElement)xParent.ParentNode.ParentNode;
            
            if (xGrandParent != null)
            {
 
                xGrandParent.InsertAfter(xNode, xParent.ParentNode);
             
            }
        }
        public void cmdCreateIteration(Window parentWindow )
        {
            

            Dictionary<string, object> sharedVariables = new Dictionary<string, object>();
            sharedVariables.Add("IterationName", IterationName);
            sharedVariables.Add("InsertMarkNode", _xInsertMark);
            sharedVariables.Add("TeamExplorer", teamExplorer);

            sharedVariables.Add("SprintWIT", Config.ConfigStore.SprintWorkItemType);
            sharedVariables.Add("StartDateField",  Config.ConfigStore.SprintStartField);
            sharedVariables.Add("FinishDateField",  Config.ConfigStore.SprintFinishField);

            sharedVariables.Add("StartDate", new DateTime(StartDate.Year, StartDate.Month, StartDate.Day, 00,00,00) );
            sharedVariables.Add("FinishDate", new DateTime(FinishDate.Year, FinishDate.Month, FinishDate.Day, 23, 59, 59));

            sharedVariables.Add("TempalteWIQLFolder", Config.ConfigStore.TempalteWIQLFolder);

            ProgressDlg progress = new ProgressDlg();
            progress.DoProgress(parentWindow, "Create new iteration" , sharedVariables, Actions);

        }

    
        private XmlElement  _xInsertMark;
        private string _SelectedIterationValurPath;

        public List<IAction> Actions { get; set; }
    } 
}
